﻿using System;
using SignalR;
using Newtonsoft.Json;
using Web.Components;
using System.Threading.Tasks;
using System.Security.Principal;
using SilenthinkContract.Domain;
using Newtonsoft.Json.Converters;
using SilenthinkCommon.Extension;
using SilenthinkContract.Component;

namespace Web.Areas.Auction.Bid
{
    public class BidConnection : PersistentConnection
    {
        private readonly BidNotifier notifier;

        public BidConnection() : this(BidNotifier.Instance) { }

        public BidConnection(BidNotifier notifier)
        {
            this.notifier = notifier;
        }
        protected override Task OnReceivedAsync(IRequest request, string connectionId, string data)
        {
            var bidIp = GetClientIp(request);
            var identity = request.User.Identity;
            var result = Bid(identity, data, bidIp);
            var json = JsonConvert.SerializeObject(result, new JavaScriptDateTimeConverter());
            return result.Status ? Connection.Broadcast(json) : Connection.Send(connectionId, json);
        }
        protected override Task OnErrorAsync(Exception error)
        {
            var result = new SkResult<string>(false, "发生异常", error.Message);
            return Connection.Broadcast(JsonConvert.SerializeObject(result));
        }
        private SkResult<Bidding> Bid(IIdentity identity, string data, string ip)
        {
            long auctionId;
            var result = new SkResult<Bidding>(false);


            if (!long.TryParse(data, out auctionId))
            {
                result.Message = "参数不正确";
            }
            else
            {
                var isAuthenticated = identity.IsAuthenticated;

                if (!isAuthenticated)
                {
                    result.Message = "请先登录";
                }
                else
                {
                    var userId = identity.SkIdentity().Id;
                    var userName = identity.Name;
                    var nickName = identity.SkIdentity().NickName;
                    if (userId == 0 && auctionId == 0)
                    {
                        result.Message = "参数不正确";
                    }
                    try
                    {
                        result = notifier.GetResult(userId, userName, nickName, ip, auctionId);
                    }
                    catch (Exception)
                    {
                        result.Status = false;
                        result.Message = "服务器异常";
                        result.Data = null;
                    }
                }
                result.Extra = auctionId;
            }
            return result;
        }

        private static string GetClientIp(IRequest request)
        {
            var result = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(result))
            {
                result = request.ServerVariables["REMOTE_ADDR"];
            }
            return result;
        }
    }

}